<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="bean.Category" %>
<%@ page import="bean.SubCategory" %>
<!DOCTYPE html>
<html>
<head>
    <title>新增商品</title>
    <style>
        body {
            background-color: #f5f5f5;
            font-family: 'Microsoft YaHei', sans-serif;
        }
        
        .container {
            max-width: 800px;
            margin: 30px auto;
            padding: 30px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        h2 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid #4CAF50;
        }
        
        .form-group {
            margin-bottom: 20px;
            position: relative;
        }
        
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
            font-size: 0.95em;
        }
        
        input[type="text"],
        input[type="number"],
        select,
        textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            transition: all 0.3s ease;
            font-size: 0.95em;
        }
        
        input[type="text"]:focus,
        input[type="number"]:focus,
        select:focus,
        textarea:focus {
            border-color: #4CAF50;
            box-shadow: 0 0 5px rgba(76,175,80,0.2);
            outline: none;
        }
        
        .btn {
            background-color: #4CAF50;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1em;
            font-weight: bold;
            width: 100%;
            margin-top: 20px;
            transition: all 0.3s ease;
        }
        
        .btn:hover {
            background-color: #45a049;
            transform: translateY(-2px);
            box-shadow: 0 2px 8px rgba(69,160,73,0.3);
        }
        
        .error {
            color: #e53935;
            margin-bottom: 20px;
            padding: 10px;
            background-color: #ffebee;
            border-radius: 4px;
            border-left: 4px solid #e53935;
        }
        
        .categories-container {
            border: 1px solid #ddd;
            padding: 20px;
            border-radius: 4px;
            background: #f9f9f9;
            margin-bottom: 25px;
        }
        
        .category-group {
            margin-bottom: 15px;
            padding: 15px;
            background: #fff;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
        }
        
        .category-group:hover {
            box-shadow: 0 2px 6px rgba(0,0,0,0.15);
        }
        
        .category-name {
            font-weight: bold;
            margin-bottom: 8px;
            color: #333;
            font-size: 1.1em;
            padding-left: 10px;
            border-left: 3px solid #4CAF50;
        }
        
        .subcategory-select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-top: 8px;
            background-color: white;
            cursor: pointer;
            height: auto;
            min-height: 100px;
        }
        
        .subcategory-select option {
            padding: 8px;
            margin: 2px 0;
        }
        
        .subcategory-select option:checked {
            background-color: #4CAF50 !important;
            color: white;
        }
        
        .subcategory-select option:hover {
            background-color: #e8f5e9;
        }
        
        input[type="file"] {
            padding: 10px;
            background-color: #f8f8f8;
            border: 2px dashed #ddd;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
            box-sizing: border-box;
        }
        
        input[type="file"]:hover {
            border-color: #4CAF50;
            background-color: #f0f7f0;
        }
        
        textarea {
            resize: vertical;
            min-height: 100px;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                margin: 15px;
                padding: 20px;
            }
            
            .category-group {
                padding: 10px;
            }
            
            .btn {
                padding: 10px 20px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>新增商品</h2>
        
        <% if(request.getAttribute("error") != null) { %>
            <div class="error">${error}</div>
        <% } %>
        
        <form action="television?action=add" method="post" enctype="multipart/form-data">
            <input type="hidden" name="categoryId" value="${param.categoryId}">
            
            <div class="form-group">
                <label for="name">商品名称:</label>
                <input type="text" id="name" name="name" required>
            </div>
            
            <div class="form-group">
                <label for="price">价格:</label>
                <input type="number" id="price" name="price" step="0.01" required>
            </div>
            
            <div class="form-group">
                <label for="screenSize">屏幕尺寸:</label>
                <input type="text" id="screenSize" name="screenSize" required>
            </div>
            
            <div class="form-group">
                <label for="resolution">分辨率:</label>
                <input type="text" id="resolution" name="resolution" required>
            </div>
            
            <div class="form-group">
                <label for="model">型号:</label>
                <input type="text" id="model" name="model" required>
            </div>
            
            <div class="form-group categories-container">
                <label>商品分类:</label>
                <% 
                    List<Category> categories = (List<Category>)request.getAttribute("categories");
                    Map<Integer, List<SubCategory>> categorySubcategories = 
                        (Map<Integer, List<SubCategory>>)request.getAttribute("categorySubcategories");
                    
                    if(categories != null) {
                        for(Category category : categories) {
                %>
                    <div class="category-group">
                        <div class="category-name"><%= category.getName() %></div>
                        <select name="subCategoryId" class="subcategory-select" multiple>
                            <option value="">请选择<%= category.getName() %>的小分类</option>
                            <% 
                                List<SubCategory> subCategories = categorySubcategories.get(category.getId());
                                if(subCategories != null) {
                                    for(SubCategory subCategory : subCategories) {
                            %>
                                <option value="<%= subCategory.getId() %>"><%= subCategory.getName() %></option>
                            <%
                                    }
                                }
                            %>
                        </select>
                    </div>
                <%
                        }
                    }
                %>
            </div>
            
            <div class="form-group">
                <label for="image">商品图片:</label>
                <input type="file" id="image" name="image" accept="image/*" required>
            </div>
            
            <div class="form-group">
                <label for="description">商品描述:</label>
                <textarea id="description" name="description" rows="4"></textarea>
            </div>
            
            <button type="submit" class="btn">保存</button>
        </form>
    </div>
    
    <script src="static/js/jquery-1.7.2.js"></script>
    <script>
    $(document).ready(function() {
        // 表单提交前验证
        $('form').on('submit', function(e) {
            let selectedCount = 0;
            $('.subcategory-select').each(function() {
                if ($(this).val() && $(this).val().length > 0) {
                    selectedCount += $(this).val().length;
                }
            });
            
            if (selectedCount < 1) {
                alert('请至少选择一个小分类');
                e.preventDefault();
                return false;
            }
        });
    });
    </script>
</body>
</html> 